package com.wn.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wn.model.entity.Book;
import com.wn.service.BookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@Api(tags = "书本数据")
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookService bookService;

    @PostMapping("/top")
    @ApiOperation("查询销量最高的前N本书籍")
    public ResponseEntity<Page<Book>> findTopSales(@RequestBody Map<String, Integer> params) {
        //获取页码和页面大小
        Integer pageNum = params.get("pageNum");
        Integer pageSize = params.get("pageSize");
        //调用业务层查询
        Page<Book> page = bookService.findTopSales(pageNum, pageSize);
        return ResponseEntity.ok(page);
    }

    @PostMapping("/findByType")
    @ApiOperation("按图书类型分页查询")
    public ResponseEntity<Page<Book>> findByType(@RequestBody Map<String, Integer> params) {
        //获取页码和页面大小
        Integer pageNum = params.get("pageNum");
        Integer pageSize = params.get("pageSize");
        Integer typeId = params.get("typeId");
        //调用业务层查询
        Page<Book> page = bookService.findByType(pageNum, pageSize,typeId);
        return ResponseEntity.ok(page);
    }

    @GetMapping("/findById/{id}")
    @ApiOperation("根据id查询书籍信息")
    public ResponseEntity<Book> findById(@PathVariable("id") Long id) {
        //调用业务层查询
        Book book = bookService.findById(id);
        return ResponseEntity.ok(book);
    }

}
